package com.vhly.appspot.util;

import java.io.*;
import java.net.HttpURLConnection;

/**
 * Created by IntelliJ IDEA.
 * User: vhly
 * Date: 2009-11-3
 * Time: 10:54:16
 *
 * @author vhly[FR]
 */
public final class StreamUtil {
    /**
     * Close Stream
     *
     * @param stream Object
     */
    public static void close(Object stream) {
        if (stream != null) {
            try {
                if (stream instanceof InputStream) {
                    ((InputStream) stream).close();
                } else if (stream instanceof OutputStream) {
                    ((OutputStream) stream).close();
                } else if (stream instanceof HttpURLConnection) {
                    ((HttpURLConnection) stream).disconnect();
                } else if (stream instanceof Writer) {
                    ((Writer) stream).close();
                } else if (stream instanceof Reader) {
                    ((Reader) stream).close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void writeStream(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[4096];
        int len;
        while (true) {
            len = in.read(buf);
            if (len == -1) {
                break;
            }
            out.write(buf, 0, len);
        }
    }

    public static byte[] readStream(InputStream in) {
        byte[] ret = null;
        if (in != null) {
            byte[] buf = new byte[64];
            int len;
            ByteArrayOutputStream bout = null;
            try {
                bout = new ByteArrayOutputStream();
                while (true) {
                    len = in.read(buf);
                    if (len == -1) {
                        break;
                    }
                    bout.write(buf, 0, len);
                }
                ret = bout.toByteArray();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } finally {
                close(bout);
            }
        }
        return ret;
    }

    public static String toHexString(byte[] buf) {
        String ret;
        StringBuffer sb = new StringBuffer();
        if (buf != null) {
            int len = buf.length;
            int v,h,l;
            for (int i = 0; i < len; i++) {
                v = buf[i];
                h = ((v & 0x0f0) >> 4);
                l = (v & 0x0f);
                if(h > 9){
                    sb.append((char)('A' + h - 10));
                }else{
                    sb.append((char)('0'+h));
                }
                if(l > 9){
                    sb.append((char)('A' + l - 10));
                }else{
                    sb.append((char)('0'+l));
                }
            }
        }
        ret = sb.toString();
        sb = null;
        return ret;
    }
}
